dojo.require("dojo.parser");
dojo.require("dijit.form.Button");
dojo.require("dijit.Dialog");
dojo.require("dijit.ProgressBar");

var results_per_page = 25;
var start_off = 0;
var end_off = results_per_page;
var num_users = 0;

function get_page_num()
{
	return (start_off/results_per_page) + 1;
}


function first_page()
{
	start_off = 0;
	end_off = results_per_page;
	if (end_off > num_users)
			end_off = num_users;
	get_lists();
	return false;
}

function last_page()
{
	var tmp = 0;
	while (tmp+results_per_page < num_users)
		tmp += results_per_page;
	start_off = tmp;
	end_off = num_users;
	get_lists();
	return false;
}

function get_total_pages()
{
	var tmp = num_users;
	var i = 0;
	while (tmp > 0)
	{
		i++;
		tmp -= results_per_page;
	}
	return i;
}

function next_page()
{
	if (end_off < num_users)
	{
		start_off += results_per_page;
		end_off = start_off + results_per_page;
		if (end_off > num_users)
			end_off = num_users;
		get_lists();
	}
	return false;
}

function prev_page()
{
	if (start_off > 0)
	{
		start_off -= results_per_page;
		if (start_off < 0)
			start_off = 0;
			
		end_off = start_off + results_per_page;
		if (end_off > num_users)
			end_off = num_users;
		
		get_lists();
	}
	return false;
}



function get_lists(){
	var table_node = dojo.byId("listtable");

	dojo.xhrPost({
		url: "index.php?pgn=user_ajax",
		content: {
			"action": "listusers",
			"start": start_off,
			"end": end_off
			},
		handleAs: "json",
		load: function(responseObject,ioArgs){
			// pagination results
			var nhtml = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"pagination\">";
			nhtml += "<tr><td width=\"59\"><input style=\"width: 59px; height: 26px;\" type=\"image\" src=\"images/first.gif\" onclick=\"javascript:return first_page()\" alt=\"First\" title=\"First\"></td>";
			nhtml += "<td width=\"25\"><input style=\"width: 25px; height: 26px;\" type=\"image\" src=\"images/previous.gif\" onclick=\"javascript:return prev_page()\" alt=\"Previous\" title=\"Previous\"></td>";
			nhtml += "<td nowrap>&nbsp;Records " + start_off + " to " + end_off + " of " + num_users + "</td>";
			nhtml += "<td nowrap>Page " + get_page_num() + " of " + get_total_pages() + "&nbsp;</td>";
			nhtml += "<td width=\"25\"><input style=\"width: 25px; height: 26px;\" type=\"image\" src=\"images/next.gif\" alt=\"Next\" onclick=\"javascript:return next_page()\" title=\"Next\"></td>";
			nhtml += "<td width=\"59\"><input style=\"width: 59px; height: 26px;\" type=\"image\" src=\"images/last.gif\" alt=\"Last\" onclick=\"javascript:return last_page()\" title=\"Last\"></td></tr></table>";
			
			// results table
			nhtml += "<table width=\"100%\" class=\"results\">";
			nhtml = nhtml + "<tr class=\"header\"><td>Name</td><td>Username</td><td>Email</td><td>Access Level</td></tr>";
			
			for (var i = 0; i < responseObject.users.length; i++) {
					nhtml += (i%2==0?"<tr class=\"dkRow\">":"<tr class=\"ltRow\">");
					nhtml += "<td>" + responseObject.users[i].realname + "</td>";
					nhtml += "<td>" + responseObject.users[i].username + "</td>";
					nhtml += "<td>" + responseObject.users[i].email + "</td>";
					
					var selname = "access_level_select" + responseObject.users[i].user_id;
					nhtml += "<td><select id=\"" + selname + "\" OnChange=\"javascript: change_user(" + 
							responseObject.users[i].user_id + ",'" + selname + "')\"" + 
							(responseObject.cur_access_level <= responseObject.users[i].access_level?" disabled":"") + ">";
					nhtml += "<option" + (responseObject.cur_access_level < 1?" disabled":"") 
									+ " value=\"1\" " + (responseObject.users[i].access_level==1?"selected":"") +  ">1 - User</option>";
					nhtml += "<option" + (responseObject.cur_access_level < 2?" disabled":"") 
									+ " value=\"2\" " + (responseObject.users[i].access_level==2?"selected":"") +  ">2 - Power User</option>";
					nhtml += "<option" + (responseObject.cur_access_level < 3?" disabled":"") 
									+ " value=\"3\" " + (responseObject.users[i].access_level==3?"selected":"") +  ">3 - Administrator</option>";
					nhtml += "</select></td>";
					
					nhtml += "</tr>";
			}
			nhtml = nhtml + "</table>";
			dojo.fadeOut({
				node: table_node,
				onEnd: function(){
					table_node.innerHTML = nhtml;
					dojo.fadeIn({node: table_node}).play();
				}
			}).play();
			return responseObject;
		},
		error: function(error,args){
			reload_on_conf_close = false;
			dojo.byId('confirmmsg').innerHTML = "Failed to Update User<br>Error Message: " + error;
			dijit.byId('confirmDialog').show();
		}
	});
}

function change_user(user_id,select_id)
{
	var select_node = dojo.byId(select_id);
	
	dojo.xhrPost({
		url: "index.php?pgn=user_ajax",
		content: {
			"action": "change_access",
			"user_id": user_id,
			"access_level": select_node.options[select_node.selectedIndex].value
			},
		handleAs: "json",
		load: function(responseObject,ioArgs){
			//get_lists();
			return responseObject;
		},
		error: function(error,args){
			reload_on_conf_close = false;
			dojo.byId('confirmmsg').innerHTML = "Failed to Update User<br>Error Message: " + error;
			dijit.byId('confirmDialog').show();
		}
	});
}

function close_confirm_dialog()
{
	if (reload_on_conf_close)
	{
		change_list_filter();
	}
	dijit.byId('confirmDialog').hide();
}

var reload_on_conf_close = false;


//num_users

var change_list_filter = function(){

	dojo.xhrPost({
		url: "index.php?pgn=user_ajax",
		content: {
			"action": "countusers"
			},
		handleAs: "json",
		load: function(responseObject,ioArgs){
			num_users = responseObject.num_users;
			first_page();
			get_lists();
			return responseObject;
		},
		error: function(error,args){
			reload_on_conf_close = false;
			dojo.byId('confirmmsg').innerHTML = "Failed to Update User Table<br>Error Message: " + error;
			dijit.byId('confirmDialog').show();
		}
	});
}

dojo.addOnLoad(change_list_filter);